/**
 * Created by WT on 2017/4/5.
 */
$(function () {
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/threeSimultaneities/projectInfo/getList.shtml',
        queryParams: {
            sort:'d.addTime',
            order:'desc'
        },
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect:true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', checkbox: true},
            {field: 'code', title: '项目编号', width: 100},
            {field: 'name', title: '项目名称', width: 100,formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'   onclick='infoFun(\""+row.id+"\");'>"+row.name+"</a>";
            }},
            {field: 'shortName', title: '项目简称', width: 100},
            {field: 'projectType', title: '项目类别', width: 100},
            {field: 'projectLevel', title: '项目级别', width: 100},
            {field: 'supervisionLevel', title: '监管级别', width: 100},
            {field: 'deptName', title: '所属部门', width: 100},
            {field: 'directorName', title: '负责人', width: 100},
            {field: 'createTime', title: '立项时间', width: 100},
            {field: 'investTime', title: '投资时间', width: 100},
            {field: 'investQuota', title: '投资额度(万元)', width: 150},
            {field:'addUserName',title:'登记人',width:100},
            {field:'addDeptName',title:'登记部门',width:100},
            {field:'addTime',title:'登记时间',width:100}
        ]]
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset');
    });
    $('#btn_search').on("click", function () {
        reLoadData();
    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    $('#btn_projectInfo_edit').on('click', function () {
        editProject();
    });
    $('#btn_projectInfo_add').on('click', function () {
        addProject();
    });
    $('#btn_projectInfo_delete').on('click', function () {
        deleteProject();
    });
    $("#searchAddUser").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }]
    });
    queryDepartmentTree();
    var PROJECT_LEVEL = CONSTANT.DictionaryTypeDefConstants.PROJECT_LEVEL;//项目级别
    queryComboData(PROJECT_LEVEL, 'projectLevelQuery');
});
var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            $("#searchAddUser").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
        }
    });
};
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('searchAddDept',result);
            deptCom('deptId',result);
        }
    });
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            prompt:'请选择',
            require:true,
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#dg1').datagrid('clearSelections');
    var queryParams1 = $('#tb1').serializeJson();
    var queryParams2 = $('#tb2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'd.addTime';
    queryParams["order"] = 'desc';
    $('#dg1').datagrid('options').queryParams = queryParams;
    $("#dg1").datagrid('reload',ROOT_PATH + '/threeSimultaneities/projectInfo/getList.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['1150px', '80%'],
        content: content,
        callBack:function(data){
            if (data){
                reLoadData();//重新加载表格
            }
        }
    });
}
function addProject(){
    var title = '新增项目';
    var content = ROOT_PATH + '/threeSimultaneities/projectInfo/add.shtml';
    openLayers(title, content);
}
function editProject(){
    var checkedRows=$('#dg1').datagrid('getChecked');
    if(checkedRows.length == 0){
        layer.alert("选择需要编辑的行！");
        return
    }
    var id=checkedRows[0].id;

    var title='编辑项目';
    var content = ROOT_PATH + '/threeSimultaneities/projectInfo/edit.shtml?id=' + id+'&type=edit';
    openLayers(title, content);
}
function infoFun(id) {
    var title='项目详情';
    var content = ROOT_PATH + '/threeSimultaneities/projectInfo/edit.shtml?id=' + id+'&type=info';
    openLayers(title, content);
}
function deleteProject(){
    var checkedRows=$('#dg1').datagrid('getChecked');
    if(checkedRows.length == 0){
        layer.alert("选择需要删除的行！");
        return
    }
    var id=checkedRows[0].id;

    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/threeSimultaneities/projectInfo/delete.shtml',
            data: {id:id},
            dataType: "json",
            success: function(data){
                if(data.success){
                    layer.msg(data.message,{time:1000,icon:1},function () {
                        $('#dg1').datagrid('reload');
                    });
                }else{
                    layer.msg(data.message,{time:1000,icon:2});
                }
            }
        });
    });
}

/**
 * 获取下拉数据
 * @param code
 * @param queryId
 */
function queryComboData(code, queryId) {
    var pageId = '#' + queryId;
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
            });
        }
    });
}